package com.a16.yuejian.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a16.yuejian.yuejian.R;
import com.a16.yuejian.yuejian.adapter.CommentAdapter;
import com.a16.yuejian.yuejian.javabean.Comment;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.model.PostModel;
import com.squareup.picasso.Picasso;

import java.util.List;

public class PostDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView post_detail_username,post_detail_time,post_detail_comment;
    private ImageView post_detail_userpic,post_detail_img,post_detail_back;
    private Intent intent;
    private Context mContext;
    private SharedPreferences sp;
    private RecyclerView recyclerview;
    private EditText post_detail_content;
    private Button post_detail_publish;

    Listener<Comment> listener = new Listener<Comment>() {
        @Override
        public void onResponse(List<Comment> t) {
          recyclerview.setAdapter(new CommentAdapter(PostDetailActivity.this,t));
        }

        @Override
        public void onFail() {

        }
    };

    SuccessListener successListener = new SuccessListener() {
        @Override
        public void onResponse(Success success) {
            if (success.getSuccess().equals("1")){
                Toast.makeText(PostDetailActivity.this,"评论成功", Toast.LENGTH_SHORT).show();
            }else if (success.getSuccess().equals("0")){
                Toast.makeText(PostDetailActivity.this,"评论失败", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onFail() {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post_detail);
        initViews();
        initEvent();
        post_detail_time.setText(getIntent().getStringExtra("time"));
        post_detail_username.setText(getIntent().getStringExtra("username"));
        Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+getIntent().getStringExtra("pic")).into(post_detail_userpic);
        Picasso.with(mContext).load("http://10.0.2.2:8080/neu_yuejian201/public/photo/"+getIntent().getStringExtra("postpic")).into(post_detail_img);
        post_detail_comment.setText(getIntent().getStringExtra("post"));
        PostModel postModel = new PostModel();
        postModel.getcomment(listener,getIntent().getStringExtra("postid"));
    }

    private void initEvent() {
        post_detail_back.setOnClickListener(this);
        post_detail_publish.setOnClickListener(this);
    }

    private void initViews() {
        post_detail_username = (TextView)findViewById(R.id.post_detail_username);
        post_detail_time = (TextView)findViewById(R.id.post_detail_time);
        post_detail_comment = (TextView)findViewById(R.id.post_detail_comment);
        post_detail_userpic = (ImageView)findViewById(R.id.post_detail_userpic);
        post_detail_img = (ImageView)findViewById(R.id.post_detail_img);
        sp = getSharedPreferences("userinfo", Context.MODE_PRIVATE);
        recyclerview = (RecyclerView)findViewById(R.id.rv_comment);
        recyclerview.setLayoutManager(new LinearLayoutManager(PostDetailActivity.this));
        post_detail_back = (ImageView)findViewById(R.id.post_detail_back);
        post_detail_publish = (Button)findViewById(R.id.post_detail_publish);
        post_detail_content = (EditText)findViewById(R.id.post_detail_content);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.post_detail_back:
                finish();
                break;
            case R.id.post_detail_publish:
                PostModel postModel = new PostModel();
                postModel.comment(sp.getString("uid",""),post_detail_content.getText().toString(),getIntent().getStringExtra("postid"),successListener);
                postModel.getcomment(listener,getIntent().getStringExtra("postid"));
                break;
        }

    }
}
